

using System.Numerics;

namespace NodeBasedSkillEditor.Model.Node.RT
{
    public class Node_Min_RT : RT_Node
    {
        public RT_SingleValuePort[] Values_RT;

        public RT_SingleValuePort Min_RT;

        public Node_Min_RT(Node_Min sourceData, RT_DAG parentDag) : base(sourceData, parentDag)
        {
            Values_RT = BuildRTPortArrayFromGroup<RT_SingleValuePort>(EPortIOType.Input, sourceData.Values);

            Min_RT = new RT_SingleValuePort(EPortIOType.Output, sourceData.Min, this);
            Min_RT.OnGetValue = GetMin;
        }

        public AValue GetMin(DAG_Executor executor)
        {
            Min_RT.SourceValue.Reset();
            if (Values_RT.Length > 0)
            {
                var valueType = Min_RT.SourceValue.ValueType;
                var first = Values_RT[0].GetValue(executor);
                if (valueType == AValue.EValueType.Integer)
                {
                    int minInt = first.ToInteger().Item2;
                    for (int i = 1; i < Values_RT.Length; i++)
                    {
                        int currInt = Values_RT[i].GetValue(executor).ToInteger().Item2;
                        if (minInt > currInt)
                        {
                            minInt = currInt;
                        }
                    }
                    Min_RT.SourceValue.SetInteger(minInt);
                }
                else if (valueType == AValue.EValueType.Scalar)
                {
                    float minfloat = first.ToScalar().Item2;
                    for (int i = 1; i < Values_RT.Length; i++)
                    {
                        float currFloat = Values_RT[i].GetValue(executor).ToScalar().Item2;
                        if (minfloat > currFloat)
                        {
                            minfloat = currFloat;
                        }
                    }
                    Min_RT.SourceValue.SetScalar(minfloat);
                }
                else if (valueType == AValue.EValueType.Vector2)
                {
                    Vector2 minVec2 = first.ToVector2().Item2;
                    for (int i = 1; i < Values_RT.Length; i++)
                    {
                        Vector2 currVec2 = Values_RT[i].GetValue(executor).ToVector2().Item2;
                        minVec2 = Vector2.Min(minVec2, currVec2);
                    }
                    Min_RT.SourceValue.SetVector2(minVec2);
                }
                else if (valueType == AValue.EValueType.Vector3)
                {
                    Vector3 minVec3 = first.ToVector3().Item2;
                    for (int i = 1; i < Values_RT.Length; i++)
                    {
                        Vector3 currVec3 = Values_RT[i].GetValue(executor).ToVector3().Item2;
                        minVec3 = Vector3.Min(minVec3, currVec3);
                    }
                    Min_RT.SourceValue.SetVector3(minVec3);
                }
            }

            return Min_RT.SourceValue;
        }
    }
}